package jwdt.xadmin.bbs.action;

import jwdt.common.action.BaseAction;
import jwdt.xadmin.bbs.bean.TBboard;
import jwdt.xadmin.bbs.bean.TBclass;
import jwdt.xadmin.bbs.bean.TBuser;

import com.ezerg.db.ext.PageUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;

@Controller
public class IndexAction extends BaseAction {
	
	private String url = "/";
	
	@RequestMapping("/index")
	public Page doMain(Form f, Module m)throws Exception {
		f.add("todaynum",jdb.queryForInt("SELECT count(*) FROM bbs_bbs1 where dateandtime>DATE_SUB(NOW(), INTERVAL '24:00:00' DAY_SECOND)"));
		f.add("ztnum",jdb.queryForInt("SELECT count(*) FROM bbs_bbs1 where parentID=0 ")).add("tznum",jdb.queryForInt("SELECT count(*) FROM bbs_bbs1"));
		f.add("usernum", jdb.queryForInt("SELECT count(*) as count FROM bbs_user "));
		f.add("huodong",jdb.query("SELECT * FROM t_images where position=2 and zt=1 order by dt desc", 1));//
		f.add("huiyi",jdb.query("SELECT * FROM bbs_bbs1 where parentID=0 and boardid=1", 3));//热门活动
		f.add("zhiding",jdb.query("SELECT * FROM bbs_bbs1 WHERE parentID=0 AND locktopic!=1 ORDER BY istop desc,announceid DESC LIMIT 10"));//置顶
		f.add("jinghua",jdb.query("SELECT * FROM bbs_bbs1 WHERE isbest=1 and parentID=0 AND locktopic!=1 ORDER BY announceid DESC LIMIT 6"));//热帖
		//f.add("classes",jdb.query("SELECT id,name as classname FROM bbs_class"));
		return m.findPage(url.concat("index"));
	}
	
	@RequestMapping("/classList")
	public Page doClassList(Form f, Module m)throws Exception {
		int page = f.getInt("page", 1);
		int pageSize = f.getInt("pageSize", 20);
		int classid = f.getInt("cid",0);
		TBclass cls = (TBclass)jdb.get(TBclass.class, classid);
		if(cls==null) {
			f.add("ret", "您访问的板块不存在");
			return m.findPage("tips");
		}
		int boardid = f.getInt("id",0);
		if(boardid==0) {
			boardid = jdb.queryForInt("select id from bbs_board where zt=1 and class="+classid+" limit 1");
			f.putText("id", boardid+"");
			f.add("id", boardid);
		}
		f.add("board", jdb.get(TBboard.class, boardid));
		f.add("boards", jdb.query("select id,name from bbs_board where zt=1 and class="+classid));
		f.addQueryString("id");
		String where = " where parentID=0 and boardid="+boardid;
		String sql = super.select_bbsnews + where + super.order_bbsnews;
		PageUtil pu = new PageUtil("select count(*) from bbs_bbs1 "+where, sql, page, pageSize);
		f.add("pager", pu.setUrl("boardList.do").setParams(f.buildQueryMap()));
		return m.findPage(url.concat("board"));
	}
	
	@RequestMapping("/boardList")
	public Page doBoardList(Form f, Module m)throws Exception {
		int page = f.getInt("page", 1);
		int pageSize = f.getInt("pageSize", 20);
		int boardid = f.getInt("id",0);
		TBboard board = (TBboard)jdb.get(TBboard.class, boardid);
		if(board==null) {
			f.add("ret", "您访问的板块不存在");
			return m.findPage("tips");
		}
		f.add("board", board);
		f.add("boards", jdb.query("select id,name from bbs_board where zt=1 and class="+board.getClassid()));
		f.addQueryString("id");
		String where = " where parentID=0 and boardid="+boardid;
		String sql = super.select_bbsnews + where + super.order_bbsnews;
		PageUtil pu = new PageUtil("select count(*) from bbs_bbs1 "+where, sql, page, pageSize);
		f.add("pager", pu.setUrl(m.getFullPath() + ".do").setParams(f.buildQueryMap()));
		return m.findPage(url.concat("board"));
	}
	
	@RequestMapping("/adminPost")
	public Page doAdminPost(Form f, Module m)throws Exception {
		TBuser userInfo = (TBuser)f.getSession("bbsUserInfo");
		if(userInfo==null || userInfo.getBbstype()!=1) {
			f.add("ret", "只有管理员有权限操作帖子");
			return m.findPage("tips");
		}
		//
		String action=f.getString("act");
		int id = f.getInt("id");
		if("isbest".equals(action)){
			jdb.execute("update bbs_bbs1 set isbest=1 where announceID="+id);
			f.add("ret","操作成功");
		}
		if("nobest".equals(action)){
			jdb.execute("update bbs_bbs1 set isbest=0 where announceID="+id);
			f.add("ret","操作成功");
		}
		if("lock".equals(action)){
			jdb.execute("update bbs_bbs1 set locktopic=1 where announceID="+id);
			f.add("ret","操作成功");
		}
		if("unlock".equals(action)){
			jdb.execute("update bbs_bbs1 set locktopic=0 where announceID="+id);
			f.add("ret","操作成功");
		}
		if("deltopic".equals(action)){
			jdb.execute("delect form bbs_bbs1 where and announceID="+id);
			f.add("ret","操作成功");
		}
		if("top".equals(action)){
			jdb.execute("update bbs_bbs1 set istop=1 where announceID="+id);
			f.add("ret","操作成功");
		}
		if("alltop".equals(action)){
			jdb.execute("update bbs_bbs1 set istop=2 announceID="+id);
			f.add("ret","操作成功");
		}
		if("untop".equals(action)||"unalltop".equals(action)){
			jdb.execute("update bbs_bbs1 set istop=0 where announceID="+id);
			f.add("ret","操作成功");
		}
		f.add("url", "detail.do?id="+id);
		//
		return m.findPage("tips");
	}
	
	
}
